package com.mmall.concurrency.example.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by RID on 2018/4/13.
 */
@Slf4j
public class CountDownLatchExample {

    private static int threadCout=200;

    public static void main(String[] args) throws Exception{
        ExecutorService service= Executors.newCachedThreadPool();
        final CountDownLatch countDownLatch=new CountDownLatch(threadCout);
        for (int i= 0;i<threadCout;i++){
            final int threadNum=i;
            service.execute(()->{

                try {
                    test(threadNum);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    countDownLatch.countDown();
                }
                //countDownLatch.countDown();

            });
        }
            countDownLatch.await();
            service.shutdown();
            log.info("finished");
    }
    private static void test(int tnum)throws Exception{
        Thread.sleep(10);
        log.info("{}",tnum);
        Thread.sleep(10);
    }
}
